package com.hejjon.controller;

import com.hejjon.core.bean.ResponseResult;
import com.hejjon.core.bean.StrListBean;
import com.hejjon.core.controller.BaseController;
import com.hejjon.core.domain.entity.DBConnection;
import com.hejjon.service.IConnectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据库连接
 * @author: cs
 * @date: 2025-02-20 14:20:40
 * @since: 1.0
 */
@RestController
@RequestMapping("/datasource/connection")
@Tag(name = "数据库连接管理", description = "用于管理数据库连接的接口")
public class ConnectionController extends BaseController {

    @Resource
    private IConnectionService connectionService;

    @PostMapping("/create")
    @Operation(summary = "创建数据库连接", description = "传入连接信息，创建新的数据库连接")
    public ResponseResult create(@RequestBody DBConnection conn) {

        boolean state = connectionService.createConn(conn);
        return toResult(state, "创建成功");
    }

    @GetMapping("/list")
    @Operation(summary = "获取连接列表", description = "返回所有已配置的数据库连接")
    public ResponseResult list() {

        return success(connectionService.findAll());
    }


    @PostMapping("/testConnect")
    @Operation(summary = "测试数据库连接", description = "测试传入的数据库连接是否有效")
    public ResponseResult testConnect(@RequestBody DBConnection conn) {

        boolean succ = connectionService.testConnect(conn);
        return toResult(succ, "连接成功");
    }


    @PostMapping("/update")
    @Operation(summary = "更新数据库连接", description = "根据传入信息更新已有连接")
    public ResponseResult update(@RequestBody DBConnection connection) {
        boolean succ = connectionService.update(connection);
        return toResult(succ, "更新成功");
    }


    /**
     * 批量删除
     * @param req
     * @return
     */
    @PostMapping("/batchRemove")
    @Operation(summary = "批量删除连接", description = "根据ID列表批量删除数据库连接")
    public ResponseResult batchRemove(@RequestBody @Validated StrListBean req) {

        return toResult(connectionService.removeByIds(req.getIds()), "连接删除成功!");
    }

}
